<!------------------------------------------------------------------------------
* rtkplot_ge.htm: rtkplot google earth plugin
*
* history: 2012/04/07 1.0  new
*------------------------------------------------------------------------------>
<html>
<head>
<title>RTKPLOT_GE</title>

<script type="text/javascript" src="http://www.google.com/jsapi"> </script>
<script type="text/javascript">

var ge = null;
var mark1,mark2,track1,track2,points,altmode;
var mark_icon1="http://maps.google.com/mapfiles/kml/paddle/ylw-circle.png";
var mark_icon2="http://maps.google.com/mapfiles/kml/paddle/red-circle.png";
var mark_icon3="http://maps.google.com/mapfiles/kml/paddle/wht-circle.png";

function init() {
    google.earth.createInstance("map3d",initCallback,failureCallback);
}

function initCallback(instance) {
    ge = instance;
    var opt = ge.getOptions();
    opt.setFlyToSpeed(ge.SPEED_TELEPORT);
    opt.setAtmosphereVisibility(false);
    opt.setMouseNavigationEnabled(true);
    opt.setBuildingSelectionEnabled(false);
    opt.setAutoGroundLevelViewEnabled(false);
    
    mark1 = GenMark('1',mark_icon1);
    mark2 = GenMark('2',mark_icon2);
    track1 = GenLine('track1','cc00ffff');
    track2 = GenLine('track2','cc0044ff');
    points = new Array();
    altmode = ge.ALTITUDE_CLAMP_TO_GROUND;
    ge.getWindow().setVisibility(true);
    
    document.getElementById('state').value='1';
    
    setInterval("UpdateState()",250);
}

function failureCallback(errcode) {
    document.write('<P align=center>Google Earth Plugin load error</P>');
    document.getElementById('state').value='-1';
}

function GenMark(name,mark_icon) {
    var pm = ge.createPlacemark('');
    var icon = ge.createIcon('');
    icon.setHref(mark_icon);
    var style = ge.createStyle('');
    style.getIconStyle().setIcon(icon);
    style.getIconstyle().getHotSpot().set(0.5,ge.UNITS_FRACTION,0,ge.UNITS_FRACTION);
    pm.setDescription(name);
    pm.setStyleSelector(style);
    ge.getFeatures().appendChild(pm);
    return pm;
}

function GenLine(name,color) {
    var pm = ge.createPlaceMark('');
    var ls = ge.createLineString('');
    var style = ge.createStyle('');
    style.getLineStyle().setWidth(3);
    style.getLineStyle().getColor().set(color);
    pm.setDescription(name);
    pm.setGeometry(ls);
    pm.setStyleSelector(style);
    ge.getFeatures().appendChild(pm);
    return pm;
}

function SetOpts(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10) {
    if (ge == null) return;
    var opt = ge.getOptions();
    ge.getNavigationControl().setVisibility(f1?ge.VISIBILITY_SHOW:ge.VISIBILITY_HIDE);
    ge.getNavigationControl().setControlType(ge.NAVIGATION_CONTROL_SMALL);
    opt.setScaleLegendVisibility(f2);
    opt.setOverviewMapVisibility(f3);
    opt.setGridVisibility(f4);
    opt.setStatusBarVisibility(f5);
    ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN,f6);
    ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS,f7);
    ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS,f8);
    ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS_LOW_RESOLUTION,f8);
    ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS,f9);
    altmode = f10?ge.ALTITUDE_ABSOLUTE:ge.ALTITUDE_CLAMP_TO_GROUND;
}

function SetView(lat,lon,range,heading) {
    if (ge == null) return;
    var la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
    la.setLatitude (lat);
    la.setLongitude(lon);
    la.setRange(range*1000.0);
    la.setHeading(heading);
    ge.getView().setAbstractView(la);
}

function SetCent(lat,lon) {
    if (ge == null) return;
    var la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
    la.setLatitude (lat);
    la.setLongitude(lon);
    ge.getView().setAbstractView(la);
}

function SetRange(range) {
    if (ge == null) return;
    var la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
    la.setRange(range*1000.0);
    ge.getView().setAbstractView(la);
}

function SetHeading(angle) {
    if (ge == null) return;
    var la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
    la.setHeading(angle);
    ge.getView().setAbstractView(la);
}

function SetTilt(angle) {
    if (ge == null) return;
    var la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
    la.setTilt(angle);
    ge.getView().setAbstractView(la);
}

function SetMark(index,lat,lon,alt) {
    if (ge == null) return;
    var mark = index==1?mark1:mark2;
    var point = ge.createPoint('');
    point.set(lat,lon,alt,altmode,1,1);
    mark.setGeometry(point);
}

function ShowMark(index) {
    if (ge == null) return;
    var mark = index==1?mark1:mark2;
    mark.setVisibility(1);
}

function HideMark(index) {
    if (ge == null) return;
    var mark = index==1?mark1:mark2;
    mark.setVisibility(0);
}

function ClearTrack(index) {
    if (ge == null) return;
    var track=index==1?track1:track2;
    var ls = track.getGeometry();
    ls.getCoordinates().clear();
}

function AddTrack(index,lat,lon) {
    if (ge == null) return;
    var track=index==1?track1:track2;
    var ls = track.getGeometry();
    ls.getCoordinates().pushLatLngAlt(lat,lon,0);
}

function ShowTrack(index) {
    if (ge == null) return;
    var track=index==1?track1:track2;
    track.setVisibility(1);
}

function HideTrack(index) {
    if (ge == null) return;
    var track=index==1?track1:track2;
    track.setVisibility(0);
}

function ClearPoint() {
    if (ge == null) return;
    for (var i = 0; i < points.length; i++) {
        ge.getFeatures().removeChild(points[i]);
    }
    points.length = 0;
}

function AddPoint(name,lat,lon,alt) {
    if (ge == null) return;
    var mark = GenMark(name,mark_icon3);
    var p = ge.createPoint('');
    p.set(lat,lon,alt,altmode,1,1);
    mark.setGeometry(p);
    points.push(mark);
}

function ShowPoint() {
    if (ge == null) return;
    for (var i = 0; i < points.length; i++) {
        var mark = points[i];
        mark.setVisibility(1);
    }
}

function HidePoint() {
    if (ge == null) return;
    for (var i = 0; i < points.length; i++) {
        var mark = points[i];
        mark.setVisibility(0);
    }
}

function UpdateState() {
    if (ge == null) return;
    var la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
    var lat=la.getLatitude ();
    var lon=la.getLongitude();
    var range=la.getRange()/1000.0;
    var heading=la.getHeading();
    document.getElementById('view').value=
        lat.toFixed(9)+","+lon.toFixed(9)+","+range.toFixed(3)+","+heading.toFixed(1);
}

google.load("earth","1");
google.setOnLoadCallback(init);

</script>
</head>

<body style="margin: 0;"; scroll="no";>
    <div id="map3d" style="height: 100%; width: 100%;"> </div>
    <input id="state" type="hidden" value="0">
    <input id="view"  type="hidden" value="0,0,0,0">
</body>
</html>
